using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using log4net;

namespace DailyReport.Core.Data.Dao.DaoFactory
{
    public class XMLReader
    {
        static ILog log = LogManager.GetLogger("XMLReader");
        public Dictionary<string,string> readXml(string filePath)
        {
            Dictionary<string,string> res = new Dictionary<string, string>();
            XPathDocument document = new XPathDocument(filePath);
            XPathNavigator navigator = document.CreateNavigator();
            XPathNodeIterator nodes = navigator.Select("/daodefinitions/dao");

            while (nodes.MoveNext())
            {
                XPathNavigator current = nodes.Current;
                current.MoveToFirstAttribute();
                string name = current.Value;
                current.MoveToNextAttribute();
                res[name] = current.Value;
            }
            return (res);
        }

        public Dictionary<string, string> readXml(XmlNode node)
        {
            //log.Debug("XMLNODE:" + node);
            Dictionary<string, string> res = new Dictionary<string, string>();
            XPathNavigator navigator = node.CreateNavigator();
            XPathNodeIterator nodes = navigator.Select("/dao-factory/dao");
            log.Debug(node.OuterXml);
            XmlElement element = node["dao-factory"];
            foreach (XmlNode childNode in element.ChildNodes)
            {
                string name = childNode.Attributes["name"].Value;
                string classname = childNode.Attributes["classname"].Value;
                res[name] = classname;
                //log.Debug("Name:"+name+ "ClassName:"+classname);
            }
            
            /*while (nodes.MoveNext())
            {
                XPathNavigator current = nodes.Current;
                current.MoveToFirstAttribute();
                string name = current.Value;
                log.Debug("Value:" + current.Value);
                current.MoveToNextAttribute();
                res[name] = current.Value;
                log.Debug("Value:" + current.Value);
            }*/
            
            return (res);
        }
    }
}
